package com.youxuit.youxu.kecheng.SynchronizedDemo;

//对象锁
public class SyncronizedObjectCodeBlock2 implements Runnable {

    public static SyncronizedObjectCodeBlock2 block2 = new SyncronizedObjectCodeBlock2();

    public Object lock1 = new Object();
    public Object lock2 = new Object();

    @Override
    public void run() {
        //1.使用当前对象作为锁
        /*synchronized (this) {
            System.out.println("我的线程名字是：" + Thread.currentThread().getName() + ",我开始运行了");

            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {

            }
            System.out.println("我的线程名字是：" + Thread.currentThread().getName() + "，运行结束了");
        }*/
        //2.使用new出的对象作为锁
       /* synchronized (lock1) {
            System.out.println("我的线程名字是：" + Thread.currentThread().getName() + ",我开始运行了");

            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {

            }
            System.out.println("我的线程名字是：" + Thread.currentThread().getName() + "，运行结束了");
        }*/
        //3.new出对象，两把锁的情况
        synchronized (lock1) {
            System.out.println("lock1我的线程名字是：" + Thread.currentThread().getName() + ",我开始运行了");

            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {

            }
            System.out.println("lock1我的线程名字是：" + Thread.currentThread().getName() + "，运行结束了");
        }

        synchronized (lock2) {
            System.out.println("lock2我的线程名字是：" + Thread.currentThread().getName() + ",我开始运行了");

            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {

            }
            System.out.println("lock2我的线程名字是：" + Thread.currentThread().getName() + "，运行结束了");
        }

    }


    public static void main(String[] args) {
        Thread t1 = new Thread(block2);
        Thread t2 = new Thread(block2);
        t1.start();
        t2.start();
        while (t1.isAlive() || t2.isAlive()) {

        }
        System.out.println("finish");
    }
}
